package com.cskaoyan.service.impl;

import com.cskaoyan.bean.bo.WxRegisterBO;
import com.cskaoyan.bean.pojo.MarketUser;
import com.cskaoyan.bean.pojo.MarketUserExample;
import com.cskaoyan.config.AliyunComponent;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.service.WxAuthService;
import com.cskaoyan.utils.Password2Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 创建日期: 2022/04/12 10:02
 *
 * @author HanWeiqi
 */
@Service
public class WxAuthServiceImpl implements WxAuthService {
    @Autowired
    AliyunComponent aliyunComponent;

    @Autowired
    MarketUserMapper marketUserMapper;


    @Override
    @Transactional
    public String regCaptcha(String mobile) {
     String code = aliyunComponent.sendMsg(mobile);
     return code;
    }

    @Override
    @Transactional
    public int register(WxRegisterBO wxRegisterBO) {
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        criteria.andUsernameEqualTo(wxRegisterBO.getUsername());
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
        if(marketUsers.size()==1){
            return 404;
        }

        MarketUser marketUser = new MarketUser();
        marketUser.setUsername(wxRegisterBO.getUsername());
        marketUser.setNickname(wxRegisterBO.getUsername());
        marketUser.setPassword(wxRegisterBO.getPassword());
        marketUser.setMobile(wxRegisterBO.getMobile());
        marketUser.setAddTime(new Date());
        marketUser.setUpdateTime(new Date());
        return marketUserMapper.insertSelective(marketUser);
    }
}
